<template>
<div class="profile-container">
    <!-- 顶部导航栏 -->
    <div class="profile-nav" :class="{ 'nav-scrolled': isNavScrolled }">
        <van-icon name="bars" size="24" @click="toggleSidebar" />

        <!-- 滚动后显示的头像和用户名 -->
        <div class="nav-user-info" v-if="isNavScrolled">
            <img :src="userInfo.avatar" class="nav-avatar" />
            <span class="nav-username">{{ userInfo.nickName || '用户昵称' }}</span>
        </div>

        <div class="nav-right">
            <van-icon name="scan" size="24" class="nav-icon" />
            <van-icon name="share-o" size="24" class="nav-icon" />
        </div>
    </div>

    <div class="profile-scroll-container" @scroll="handleScroll">

        <!-- 用户信息区域 -->
        <div class="profile-info">
            <div class="user-header">
                <div class="avatar-container">
                    <img :src="userInfo.avatar" class="avatar" />
                    <div class="edit-avatar" @click="editAvatar">
                        <van-icon name="plus" size="20" color="#fff" />
                    </div>
                </div>

                <div class="user-basic-info">
                    <div class="username">{{ userInfo.nickName || '纵横' }}</div>
                    <div class="user-id">
                        小红书号：{{ userInfo.userId || '4286474693' }}
                        <van-icon name="description" size="14" />
                    </div>
                </div>
            </div>

            <div class="bio-placeholder" @click="editProfile">点击这里，填写简介</div>

            <!-- 用户数据统计 -->
            <div class="user-stats">
                <div class="stat-item" @click="viewFollows('following')">
                    <div class="stat-num">{{ userInfo.followingCount }}</div>
                    <div class="stat-label">关注</div>
                </div>
                <div class="stat-item" @click="viewFollows('followers')">
                    <div class="stat-num">{{ userInfo.followerCount }}</div>
                    <div class="stat-label">粉丝</div>
                </div>
                <div class="stat-item">
                    <div class="stat-num">{{ userInfo.likeCount }}</div>
                    <div class="stat-label">获赞与收藏</div>
                </div>
                <!-- 编辑资料按钮 -->
                <div class="stat-item" @click="editProfile">
                    <div>编辑资料</div>
                </div>
                <div class="stat-item" @click="goToSettings">
                    <van-icon name="setting-o" />
                </div>
            </div>
        </div>

        <!-- 内容标签页 -->
        <div class="content-area">
            <!-- 标签页导航 -->
            <van-sticky :offset-top="50" container=".profile-scroll-container">
                <div class="tab-header">
                    <div class="tab-item" :class="{ active: activeTab === 0 }" @click="switchTab(0)">
                        <span>笔记</span>
                        <div class="active-line" v-if="activeTab === 0"></div>
                    </div>
                    <div class="tab-item" :class="{ active: activeTab === 1 }" @click="switchTab(1)">
                        <span>收藏</span>
                        <div class="active-line" v-if="activeTab === 1"></div>
                    </div>
                    <div class="tab-item" :class="{ active: activeTab === 2 }" @click="switchTab(2)">
                        <span>赞过</span>
                        <div class="active-line" v-if="activeTab === 2"></div>
                    </div>
                </div>
            </van-sticky>

            <!-- 笔记列表 -->
            <div class="tab-content">
                <div v-show="activeTab === 0" class="notes-list">
                    <div class="note-item" v-for="(note, index) in notes" :key="index">
                        <div class="note-cover">
                            <img :src="note.cover" :alt="note.title" />
                            <div class="note-views">
                                <van-icon name="eye-o" />
                                <span>{{ note.views }}</span>
                            </div>
                        </div>
                        <div class="note-title">{{ note.title }}</div>
                        <div class="note-author">
                            <img :src="userInfo.avatar" class="author-avatar" />
                            <span class="author-name">{{ userInfo.username }}</span>
                            <div class="note-likes">
                                <van-icon name="like-o" />
                                <span>{{ note.likes }}</span>
                            </div>
                        </div>
                    </div>
                </div>

                <div v-show="activeTab === 1" class="notes-list">
                    <div class="empty-content">暂无收藏内容</div>
                </div>

                <div v-show="activeTab === 2" class="notes-list">
                    <div class="empty-content">暂无点赞内容</div>
                </div>
            </div>
        </div>
    </div>
</div>
</template>

<script>
import { mapGetters } from 'vuex'
import { formatNumber } from '@/utils'
import { getUserInfo, getMyNotes } from '@/api/user'
import { Toast, Sticky } from 'vant'

export default {
    name: 'ProfilePage',
    components: {
        vanSticky: Sticky
    },
    data() {
        return {
            userInfo: {
                username: '',
                userId: '',
                avatar: '',
                followingCount: 0,
                followerCount: 0,
                likeCount: 0,
                bio: ''
            },
            notes: [],
            loading: false,
            activeTab: 0,
            scrollTop: 0,
            isNavScrolled: false
        }
    },

    computed: {
        ...mapGetters(['user', 'isLogin'])
    },

    created() {
        this.fetchUserProfile()
        this.fetchUserNotes()
    },

    mounted() {
        // 初始化滚动监听
        const scrollContainer = this.$el.querySelector('.profile-scroll-container');
        if (scrollContainer) {
            scrollContainer.addEventListener('scroll', this.handleScroll);
        }
    },

    beforeDestroy() {
        // 移除滚动监听
        const scrollContainer = this.$el.querySelector('.profile-scroll-container');
        if (scrollContainer) {
            scrollContainer.removeEventListener('scroll', this.handleScroll);
        }
    },

    methods: {
        formatNumber,

        // 切换标签页
        switchTab(index) {
            this.activeTab = index;
        },

        // 切换侧边栏显示状态
        toggleSidebar() {
            // 调用App.vue中的toggleSidebar方法
            this.$parent.toggleSidebar()
        },

        // 获取用户个人资料
        async fetchUserProfile() {
            try {
                this.loading = true
                const data = await getUserInfo()
                this.userInfo = data.user
                this.userInfo.avatar = require("@/assets/images/profile.jpg");
                this.userInfo.nickName = '纵横'; // 默认昵称
                this.userInfo.userId = '4286474693'; // 默认小红书号
                this.userInfo.followerCount = 111;
                this.userInfo.followingCount = 222;
                this.userInfo.likeCount = 333;
            } catch (error) {
                console.error('获取用户资料失败:', error)
                Toast.fail('获取用户资料失败')
            } finally {
                this.loading = false
            }
        },

        // 获取用户笔记列表
        async fetchUserNotes() {
            try {
                this.loading = true
                const data = await getMyNotes()
                this.notes = data || []

                // 如果没有笔记数据，添加一些模拟数据
                if (!this.notes.length) {
                    this.notes = [
                        {
                            id: 1,
                            title: '探店成都必吃的网红咖啡店',
                            cover: 'https://picsum.photos/400/400?random=1',
                            views: '2.1w',
                            likes: '435'
                        },
                        {
                            id: 2,
                            title: '穿搭分享|夏日清凉风格',
                            cover: 'https://picsum.photos/400/400?random=2',
                            views: '1.5w',
                            likes: '326'
                        },
                        {
                            id: 3,
                            title: '超级好用的护肤品推荐',
                            cover: 'https://picsum.photos/400/400?random=3',
                            views: '3.2w',
                            likes: '567'
                        },
                        {
                            id: 4,
                            title: '周末美食|自制奶茶教程',
                            cover: 'https://picsum.photos/400/400?random=4',
                            views: '4.6w',
                            likes: '789'
                        },
                        {
                            id: 5,
                            title: '旅行日记|北京三日游',
                            cover: 'https://picsum.photos/400/400?random=5',
                            views: '2.8w',
                            likes: '456'
                        }
                    ]
                }
            } catch (error) {
                console.error('获取笔记列表失败:', error)
                Toast.fail('获取笔记列表失败')
            } finally {
                this.loading = false
            }
        },

        editAvatar() {
            this.$router.push('/profile-edit')
        },

        editProfile() {
            this.$router.push('/profile-edit')
        },

        viewFollows(type) {
            this.$router.push(`/follows/${type}`)
        },

        goToSettings() {
            this.$router.push('/settings')
        },

        handleScroll(event) {
            // 获取滚动位置
            const scrollTop = event.target.scrollTop;
            this.scrollTop = scrollTop;
            // 获取头像容器的位置
            const avatarContainer = this.$el.querySelector('.avatar-container');
            if (avatarContainer) {
                const avatarRect = avatarContainer.getBoundingClientRect();
                // 获取头像底部位置
                const avatarBottom = avatarRect.top + avatarRect.height;
                // 导航栏高度约为50px，判断头像底部是否与导航栏底部重合或即将重合
                this.isNavScrolled = avatarBottom <= 50;
            }
        }
    }
}
</script>

<style scoped>
.profile-container {
    height: 100vh;
    background-color: #f7f8fa;
    position: relative;
    overflow: hidden;
}

.profile-scroll-container {
    height: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 50px;
}

.profile-nav {
    align-items: center;
    padding: 10px 16px;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-between;
    z-index: 100;
    color: #fff;
    transition: all 0.3s ease;
}

.nav-scrolled {
    background-color: #a2c9cf;
    color: #FFF;
}

.nav-right {
    display: flex;
}

.nav-icon {
    margin-left: 16px;
}

.nav-user-info {
    display: flex;
    align-items: center;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

.nav-avatar {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    margin-right: 8px;
    border: 1px solid #f0f0f0;
}

.nav-username {
    font-size: 14px;
    font-weight: 500;
    color: #fff;
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.profile-info {
    position: relative;
    padding: 80px 16px 36px;
    margin-bottom: -15px;
    background-image:
        linear-gradient(to bottom,
            rgba(0, 0, 0, 0.3) 0%,
            rgba(0, 0, 0, 0.2) 50%,
            rgba(0, 0, 0, 0.1) 80%,
            rgba(247, 248, 250, 0) 100%),
        url("@/assets/profilebackgroundimage.jpg");
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    min-height: 220px;
}

.user-header {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
}

.avatar-container {
    position: relative;
    width: 80px;
    height: 80px;
    margin-right: 16px;
    flex-shrink: 0;
}

.avatar {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    border: 2px solid #fff;
    object-fit: cover;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.edit-avatar {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 24px;
    height: 24px;
    background-color: #ffde45;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.user-basic-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.username {
    font-size: 24px;
    font-weight: 700;
    color: #ffffff;
    margin-bottom: 6px;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.user-id {
    font-size: 13px;
    color: rgba(255, 255, 255, 0.9);
    display: flex;
    align-items: center;
}

.user-id .van-icon {
    margin-left: 6px;
    color: rgba(255, 255, 255, 0.9);
}

.bio-placeholder {
    font-size: 14px;
    color: rgba(255, 255, 255, 0.8);
    padding: 4px 0 12px;
}

.user-stats {
    display: flex;
}

.stat-item {
    margin-right: 24px;
    text-align: center;
    align-content: center;
}

.stat-num {
    font-size: 18px;
    font-weight: 600;
    color: #ffffff;
}

.stat-label {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.8);
}

/* 内容区域样式 */
.content-area {
    background-color: #fff;
    border-radius: 20px 20px 0 0;
    overflow: hidden;
    min-height: calc(100vh - 12.2vh);
    position: relative;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
    margin-top: -15px;
}

/* 标签页导航样式 */
.tab-header {
    display: flex;
    border-bottom: 1px solid #f5f5f5;
    background-color: #fff;
    border-radius: 20px 20px 0 0;
    overflow: hidden;
}

.tab-item {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 44px;
    font-size: 14px;
    color: #666;
    position: relative;
    cursor: pointer;
}

.tab-item.active {
    color: #333;
    font-weight: 500;
}

.active-line {
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 20px;
    height: 3px;
    background-color: #ff2442;
    border-radius: 3px;
}

/* 标签页内容样式 */
.tab-content {
    min-height: calc(100vh - 300px);
    /* 确保有足够的滚动空间 */
}

.notes-list {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    padding: 16px;
}

.note-item {
    display: flex;
    flex-direction: column;
}

.note-cover {
    position: relative;
    border-radius: 8px;
    overflow: hidden;
    margin-bottom: 8px;
}

.note-cover img {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
}

.note-views {
    position: absolute;
    bottom: 8px;
    left: 8px;
    background-color: rgba(0, 0, 0, 0.5);
    color: #fff;
    font-size: 12px;
    padding: 2px 6px;
    border-radius: 10px;
    display: flex;
    align-items: center;
}

.note-views .van-icon {
    margin-right: 4px;
    font-size: 12px;
}

.note-title {
    font-size: 14px;
    color: #333;
    margin-bottom: 8px;
    font-weight: 500;
    line-height: 1.4;
    /* 文本超出两行显示省略号 */
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
}

.note-author {
    display: flex;
    align-items: center;
    margin-top: auto;
}

.author-avatar {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    margin-right: 6px;
}

.author-name {
    font-size: 12px;
    color: #666;
    flex: 1;
    /* 文本超出显示省略号 */
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.note-likes {
    display: flex;
    align-items: center;
    font-size: 12px;
    color: #999;
}

.note-likes .van-icon {
    margin-right: 2px;
}

.empty-content {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 200px;
    color: #999;
    font-size: 14px;
}

/* 编辑资料按钮样式 */
.stat-item div {
    color: #fff;
    font-size: 14px;
}

/* 为了处理sticky状态下的样式 */
:deep(.van-sticky--fixed) .tab-header {
    border-radius: 0;
}
</style>